Skip to content

Derive log file path from BASE_DIR, degrade gracefully if unwritable (#1283)#1415

Open
jonfroehlich wants to merge 1 commit into
masterfrom
1283-derive-log-path-from-base-dir
Open

Derive log file path from BASE_DIR, degrade gracefully if unwritable (#1283)#1415
jonfroehlich wants to merge 1 commit into
masterfrom
1283-derive-log-path-from-base-dir

Conversation

@jonfroehlich

Copy link
Copy Markdown
Member

Fixes #1283.

Problem

The LOGGING file handler in makeabilitylab/settings.py hardcoded an absolute, container-specific path:

'filename': '/code/media/debug.log',

Django evaluates LOGGING at django.setup(), so on any host lacking that exact directory (e.g. GitHub Actions CI) startup died with FileNotFoundError before a single request or test ran. This was papered over in settings_test.py (swapping the handler for a NullHandler), but the source-level fragility remained for any non-/code deploy or tooling.

Fix

Combines the issue's Option 1 (derive from BASE_DIR) and Option 2 (degrade gracefully):

  • LOG_DIR = os.environ.get('ML_LOG_DIR', os.path.join(BASE_DIR, 'media')), LOG_FILE = LOG_DIR/debug.log.
  • os.makedirs(LOG_DIR, exist_ok=True) and check writability; if the dir can't be created or written, the file handler falls back to logging.NullHandler so startup never dies over a log path.
  • Adds an ML_LOG_DIR env override for future non-/code hosts.

Deliberately does not relocate the log out of MEDIA_ROOT (the issue's Option 3) — the /logs/ exposure is intentional (docs/DEPLOYMENT.md) and relocating needs CSE IT coordination.

No behavior change on servers/local dev

In the container BASE_DIR is /code, so the default still resolves to /code/media/debug.log. Prod, test, and local dev are unaffected. The NullHandler swap in settings_test.py is now belt-and-suspenders rather than the only crash guard.

Verification

  • Normal path → RotatingFileHandler at /code/media/debug.log, writable.
  • ML_LOG_DIR=/nonexistent/cannot/create → degrades to NullHandler, django.setup() succeeds, logging still functions.
  • manage.py check --settings=makeabilitylab.settings_test → no issues.

🤖 Generated with Claude Code

…1283)

The LOGGING 'file' handler hardcoded an absolute, container-specific path
(/code/media/debug.log). Django evaluates LOGGING at django.setup(), so on
any host lacking that exact directory (e.g. GitHub Actions CI) startup died
with FileNotFoundError before a single request or test ran.

Derive the path from BASE_DIR instead (still under media/ so it stays
reachable via the intentional /logs/ URL per docs/DEPLOYMENT.md), allow an
ML_LOG_DIR env override, and fall back to a NullHandler when the log dir
can't be created or written so a bad log path never crashes startup.

In the container/servers BASE_DIR is /code, so the default still resolves to
/code/media/debug.log — no behavior change on prod, test, or local dev. The
NullHandler swap in settings_test.py is now belt-and-suspenders rather than
the only crash guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

settings.py hardcodes the log file to /code/media/debug.log (breaks on any host without that path)

1 participant